import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_layout/base/index.dart';
import 'package:flutter_layout/main/base_stateful_widget.dart';

class RefreshViewPage extends BaseStatefulWidget {
  const RefreshViewPage({Key? key}) : super(key: key);

  @override
  _ConstrainPageState createState() => _ConstrainPageState();
}

class _ConstrainPageState extends State<RefreshViewPage> {
  int currentIndex = 0;
  PageController pageController = PageController();

  var pageLsit = [
    const CachePage(
      title: "1",
    ),
    const CachePage(
      title: "2",
    ),
  ];
  var titleTool = [
    "RefreshIndicator",
    "CupertinoSliverRefreshControl",
  ];
  String? toolTitle;

  @override
  Widget build(BuildContext context) {
    var bottomItem = <BottomNavigationBarItem>[];
    for (var i = 0; i < pageLsit.length; i++) {
      bottomItem.add(BottomNavigationBarItem(icon: const Icon(Icons.access_alarm_rounded), label: "$i"));
    }
    return Scaffold(
      appBar: AppBar(
        title: Text(toolTitle ?? titleTool[0]),
      ),
      body: PageView(
        physics: const PageScrollPhysics(),
        controller: pageController,
        children: pageLsit,
        onPageChanged: (value) {
          currentIndex = value;
          _changeItem(currentIndex);
        },
      ),
      bottomNavigationBar: BottomNavigationBar(
        onTap: (value) {
          setState(() {
            currentIndex = value;
            showToast("$currentIndex");
            pageController.jumpToPage(currentIndex);
          });
        },
        type: BottomNavigationBarType.fixed,
        currentIndex: currentIndex,
        // backgroundColor: Colors.yellow,
        items: bottomItem,
      ),
    );
  }

  void _changeItem(int value) {
    setState(() {
      currentIndex = value;
      toolTitle = titleTool[currentIndex];
    });
  }
}

class CachePage extends StatefulWidget {
  final String? title;
  const CachePage({Key? key, this.title}) : super(key: key);

  @override
  _CachePageState createState() => _CachePageState();
}

class _CachePageState extends State<CachePage>
//  with AutomaticKeepAliveClientMixin
{
  // String? title;
  // _CachePageState({this.title});
  @override
  Widget build(BuildContext context) {
    // super.build(context);
    if ("1" == widget.title) {
      return getRefreshIndicator();
    }
    return getCupertinoSliverRefreshControl();
  }

  // ignore: unused_element
  _getPhysics(String? title) {
    return const BouncingScrollPhysics();
  }

  final _list = [1, 2, 3, 4, 5];
  Widget getRefreshIndicator() {
    showToast(_list.length.toString());
    return Scaffold(
      body: RefreshIndicator(
        displacement: 10,
        color: Colors.red,
        backgroundColor: Colors.lightBlue,
        onRefresh: () async {
          setState(() {
            _list.add(_list.length + 1);
            logger.e("length=${_list.length}");
          });
        },
        child: ListView.builder(
          itemBuilder: (context, index) {
            return ListTile(
              title: Text('老孟${_list[index]}'),
            );
          },
          itemExtent: 50,
          itemCount: _list.length,
        ),
      ),
    );
  }

  Widget getCupertinoSliverRefreshControl() {
    // return Scaffold(
    //   body: PopupMenuButton<String>(
    //     itemBuilder: (context) {
    //       return <PopupMenuEntry<String>>[
    //         PopupMenuItem<String>(
    //           value: '语文',
    //           child: Text('语文'),
    //         ),
    //         PopupMenuItem<String>(
    //           value: '数学',
    //           child: Text('数学'),
    //         ),
    //         PopupMenuItem<String>(
    //           value: '英语',
    //           child: Text('英语'),
    //         ),
    //         PopupMenuItem<String>(
    //           value: '生物',
    //           child: Text('生物'),
    //         ),
    //         PopupMenuItem<String>(
    //           value: '化学',
    //           child: Text('化学'),
    //         ),
    //       ];
    //     },
    //   ),
    // );
    // return CupertinoButton(child: Text("asdfasdf"), onPressed: (){});
    // return Scaffold(
    return CustomScrollView(
      slivers: <Widget>[
        CupertinoSliverRefreshControl(
          onRefresh: () async {
            setState(() {
              _list.add(_list.length + 1);
            });
          },
        ),
        SliverList(
          delegate: SliverChildBuilderDelegate((content, index) {
            return ListTile(
              title: Text('老孟${_list[index]}'),
            );
          }, childCount: _list.length),
        )
      ],
      // ),
    );
  }

  // @override
  // bool get wantKeepAlive => true;
}
